const app = getApp()
const db = wx.cloud.database();

let postid = null;
let postindex = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wdfImgArr: [],

    // 声明一个tempObject存放用户个人中心的游戏数据
    UserGameInfo: {},

    // 帖子/动态区
    UserGameInfo: {},
    wdfImgArr: [],
    openOrClose: false,
    comments: [],
  },

  
  getUserGameInfo(){
    db.collection("userInfo").where({
      _openid: app.currentUserOpenid
    }).get().then(res=>{
      this.setData({
        UserGameInfo: res.data[0]
      })
    })
  },
  
  getUserOpenId(){
    wx.cloud.callFunction({
      name: 'getOpenId',
      complete: res => {
        app.currentUserOpenid = res.result.openid

        // 显示全部帖子
        // wx.cloud.callFunction({
        //   name: 'getPost',
        // }).then(res2 => {
        //   let tempImgArr = []
        //   res2.result.data.forEach(
        //     (item,index)=>{
        //       tempImgArr.push(item)
        //     }
        //   )
        //   this.setData({
        //     wdfImgArr: tempImgArr
        //   })
        //   // console.log("wdfImgArr 用来渲染",this.data.wdfImgArr[0].comments);
        // })
        // .catch(console.error)

        // 等下可以放到“个人中心页面”的 我的动态那里
        db.collection("publishPosts").get().then(res2=>{
          // console.log(res2.data);
          let tempImgArr = []
          res2.data.forEach(
            (item,index)=>{
              tempImgArr.push(item)
            }
          )
          this.setData({
            wdfImgArr: tempImgArr
          })
          // console.log("wdfImgArr 用来渲染",this.data.wdfImgArr[0]);
        })
      }
    })
  },

  // 打开评论区————————————————————————————————————————————
  openComment(e){
    // console.log(e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id
    if (this.data.openOrClose == false) {
      wx.cloud.callFunction({
        name: "getPostByid",
        data:{
          id,
        }
      }).then(res2=>{
        // console.log(res2);
        // let tempComments = res2.result.data.comments
        this.setData({
          commentBox: "block",
          openOrClose: true,
          borderBottom: "6rpx solid red;",
          comments: res2.result.data.comments
        })
        // console.log("this.data.comments",this.data.comments);
      })
    }else {
      this.setData({
        commentBox: "none",
        openOrClose: false,
        borderBottom: "none"
      })
    }
    this.onLoad()
  },

  deletePost(e){
    console.log(e.currentTarget.dataset.id);
    wx.showModal({
      title: '提示',
      content: '确定要删除该动态吗？',
      success:(res3)=>{
        if (res3.confirm) {
          console.log("用户点击确定");
          let currentPostId = e.currentTarget.dataset.id
          wx.cloud.callFunction({
            name: "deletePost",
            data:{
              id: currentPostId,
            }
          }).then(res=>{
            wx.showToast({
              title: '删除成功',
            })
          })
          db.collection("publishPosts").get().then(res2=>{
            // console.log(res2.data);
            let tempImgArr = []
            res2.data.forEach(
              (item,index)=>{
                tempImgArr.push(item)
              }
            )
            this.setData({
              wdfImgArr: tempImgArr
            })
            // console.log("wdfImgArr 用来渲染",this.data.wdfImgArr[0]);
          })
          this.onLoad()
        } else if (res3.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserGameInfo()
    this.getUserOpenId()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  backShow() {
    wx.switchTab({
      url: '../../../pages/users/users',
    })
  },
})